﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.UI.HtmlControls;
using NextCMS.Models;

public partial class ViewMode : BasePage
{
    public string fileName = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        TopFixedMenu1.UserLanguageId = UserLanguageId;
        TopFixedMenu1.FrontSiteId = FrontSiteId;
        TopFixedMenu1.IsLayoutMode = false;
        fileName = Convert.ToString(Page.RouteData.Values["fileName"]);
        if (!String.IsNullOrEmpty(fileName))
        {
            PagesHelper pc = new PagesHelper();
            PageModel pm = pc.GetPageByFileName(fileName,FrontSiteId,UserLanguageId);
            if (pm != null)
            {
                TemplatesHelper mpc = new TemplatesHelper();
                TemplateModel mpm = mpc.GetById(pm.MasterPageId);
                SiteResourcesHelper srh = new SiteResourcesHelper();
                SiteResourceModel srm = srh.GetById(mpm.WebResourceId);
                LoadCss(srm.FolderName);
                LoadJavascript(srm.FolderName);
                //ltrContent.Text = mpm.Content;
                foreach (PageSections ps in mpm.lstPageSections)
                {
                    mpm.Content = mpm.Content.Replace("[{" + ps.Name + "}]", GetHTMLForSections(pm.Id, ps.Id, ps.Name));
                }
                myPlaceholder.Controls.Add(ParseControl(mpm.Content));
                Page.Title = pm.Title;

                HtmlGenericControl hc = new HtmlGenericControl("meta");
                hc.Attributes.Add("name", "keywords");
                hc.Attributes.Add("content", pm.MetaKeywords);
                Page.Header.Controls.Add(hc);

                hc = new HtmlGenericControl("meta");
                hc.Attributes.Add("name", "description");
                hc.Attributes.Add("content", pm.MetaDescription);
                Page.Header.Controls.Add(hc);
            }
        }
        Page.ClientScript.RegisterClientScriptInclude(Page.GetType(), "jquery-1.7.js", ResolveClientUrl("~/Resources/Javascript/jquery-1.7.js"));
        if (Session["Username"] != null)
        {
            Page.ClientScript.RegisterClientScriptInclude(Page.GetType(), "jquery.jixedbar.js", ResolveClientUrl("~/Resources/Javascript/jquery.jixedbar.js"));
            string js = @"$('.topFixedMenu').jixedbar({
          showOnTop: true,
          transparent: true,
          opacity: 0.5,
          slideSpeed: 'slow',
          roundedCorners: false,
          roundedButtons: false,
          menuFadeSpeed: 'slow',
          tooltipFadeSpeed: 'fast',
          tooltipFadeOpacity: 0.5
      });";
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "cc", js, true);
        }
        else
        {
            TopFixedMenu1.Visible = false;
        }
        
    }
    string GetHTMLForSections(int PageId, Guid SectionId, string SectionName)
    {
        PageModulesHelper pm = new PageModulesHelper();
        IEnumerable<PageModuleModel> lstPageModules = pm.GetById(PageId, SectionId, UserLanguageId);
        string ModuleHTML = "";
        foreach (PageModuleModel pmm in lstPageModules)
        {
            ModuleHTML = ModuleHTML +GetModuleDetails(pmm);
        }
        string HTML = ModuleHTML;
        return HTML;
    }
    string GetModuleDetails(PageModuleModel pmm)
    {
        ModulesHelper mc = new ModulesHelper();
        ModuleModel mm = mc.GetById(pmm.ModuleId);
        string TypeName = "";
        string Settings = "";
        if (pmm.SubModuleId > 0)
        {
            SubModule sm = mm.SubModules.Where(c => c.Id == pmm.SubModuleId).FirstOrDefault();
            TypeName = sm.Type;
            Settings = sm.Settings;
        }
        else if (pmm.ModuleId > 0)
        {
            TypeName = mm.Type;
            Settings = mm.Settings;
        }
        Type type = typeof(Modules.IPageModule).Assembly.GetType("Modules." + TypeName + "");
        //object newObject = typeof(Modules.Module).GetMethod("Initialize").MakeGenericMethod(type).Invoke(null, null);

        object HTML = type.GetMethod("GetViewModeHTML").Invoke(Activator.CreateInstance(type), new object[] { pmm, mm, UserLanguageId });
        return HTML as string;
    }
}